function BxInterfaceForm(t,e){var s=this;this.name=t;this.aTabs=e;this.bExpandTabs=false;this.vars={};this.oTabsMeta={};this.aTabsEdit=[];this.oFields={};this.menu=new PopupMenu("bxFormMenu_"+this.name,1010);this.settingsMenu=[];this.tabSettingsWnd=null;this.fieldSettingsWnd=null;this.SelectTab=function(t){var e=document.getElementById("inner_tab_"+t);if(e.style.display!="none")return;for(var s=0,i=this.aTabs.length;s<i;s++){var a=document.getElementById("inner_tab_"+this.aTabs[s]);if(a.style.display!="none"){this.ShowTab(this.aTabs[s],false);a.style.display="none";break}}this.ShowTab(t,true);e.style.display="block";var n=document.getElementById(this.name+"_active_tab");if(n)n.value=t};this.ShowTab=function(t,e){var s=e?"-selected":"";document.getElementById("tab_cont_"+t).className="bx-tab-container"+s;document.getElementById("tab_left_"+t).className="bx-tab-left"+s;document.getElementById("tab_"+t).className="bx-tab"+s;document.getElementById("tab_right_"+t).className="bx-tab-right"+s};this.HoverTab=function(t,e){var s=document.getElementById("tab_"+t);if(s.className=="bx-tab-selected")return;document.getElementById("tab_left_"+t).className=e?"bx-tab-left-hover":"bx-tab-left";s.className=e?"bx-tab-hover":"bx-tab";var i=document.getElementById("tab_right_"+t);i.className=e?"bx-tab-right-hover":"bx-tab-right"};this.ShowDisabledTab=function(t,e){var i=document.getElementById("tab_cont_"+t);if(e){i.className="bx-tab-container-disabled";i.onclick=null;i.onmouseover=null;i.onmouseout=null}else{i.className="bx-tab-container";i.onclick=function(){s.SelectTab(t)};i.onmouseover=function(){s.HoverTab(t,true)};i.onmouseout=function(){s.HoverTab(t,false)}}};this.ToggleTabs=function(t){this.bExpandTabs=!this.bExpandTabs;var e=document.getElementById("bxForm_"+this.name+"_expand_link");if(!e){return}e.title=this.bExpandTabs?this.vars.mess.collapseTabs:this.vars.mess.expandTabs;e.className=this.bExpandTabs?e.className.replace(/\s*bx-down/gi," bx-up"):e.className.replace(/\s*bx-up/gi," bx-down");var s;for(var i in this.aTabs){var a=this.aTabs[i];this.ShowTab(a,false);this.ShowDisabledTab(a,this.bExpandTabs);s=document.getElementById("inner_tab_"+a);s.style.display=this.bExpandTabs?"block":"none"}if(!this.bExpandTabs){this.ShowTab(this.aTabs[0],true);s=document.getElementById("inner_tab_"+this.aTabs[0]);s.style.display="block"}if(t!==true)BX.ajax.get("/bitrix/components"+this.vars.component_path+"/settings.php?FORM_ID="+this.name+"&action=expand&expand="+(this.bExpandTabs?"Y":"N")+"&sessid="+this.vars.sessid)};this.SetTheme=function(t,e){BX.loadCSS(this.vars.template_path+"/themes/"+e+"/style.css");var i=this.menu.GetMenuByItemId(t.id);i.SetAllItemsIcon("");i.SetItemIcon(t,"checked");BX.ajax.get("/bitrix/components"+s.vars.component_path+"/settings.php?FORM_ID="+this.name+"&GRID_ID="+this.vars.GRID_ID+"&action=settheme&theme="+e+"&sessid="+this.vars.sessid)};this.ShowSettings=function(){var t=false;if(!window["formSettingsDialog"+this.name]){window["formSettingsDialog"+this.name]=new BX.CDialog({content:'<form name="form_settings_'+this.name+'"></form>',title:this.vars.mess.settingsTitle,width:this.vars.settingWndSize.width,height:this.vars.settingWndSize.height,resize_id:"InterfaceFormSettingWnd"});t=true}window["formSettingsDialog"+this.name].ClearButtons();window["formSettingsDialog"+this.name].SetButtons([{title:this.vars.mess.settingsSave,action:function(){s.SaveSettings();this.parentWindow.Close()}},BX.CDialog.prototype.btnCancel]);window["formSettingsDialog"+this.name].Show();var e=document["form_settings_"+this.name];if(t)e.appendChild(BX("form_settings_"+this.name));var i;this.aTabsEdit=[];for(i in this.oTabsMeta){var a=[];for(var n in this.oTabsMeta[i].fields)a[a.length]=BX.clone(this.oTabsMeta[i].fields[n]);this.aTabsEdit[this.aTabsEdit.length]=BX.clone(this.oTabsMeta[i]);this.aTabsEdit[this.aTabsEdit.length-1].fields=a}jsSelectUtils.deleteAllOptions(e.tabs);for(i in this.aTabsEdit)e.tabs.options[e.tabs.length]=new Option(this.aTabsEdit[i].name,this.aTabsEdit[i].id,false,false);e.tabs.selectedIndex=0;this.OnSettingsChangeTab();this.aAvailableFields=BX.clone(this.oFields);jsSelectUtils.deleteAllOptions(e.all_fields);for(i in this.aAvailableFields)e.all_fields.options[e.all_fields.length]=new Option(this.aAvailableFields[i].name,this.aAvailableFields[i].id,false,false);jsSelectUtils.sortSelect(e.all_fields);this.HighlightSections(e.all_fields);this.ProcessButtons();e.tabs.focus()};this.OnSettingsChangeTab=function(){var t=document["form_settings_"+this.name];var e=t.tabs.selectedIndex;jsSelectUtils.deleteAllOptions(t.fields);for(var s in this.aTabsEdit[e].fields){var i=new Option(this.aTabsEdit[e].fields[s].name,this.aTabsEdit[e].fields[s].id,false,false);if(this.aTabsEdit[e].fields[s].type=="section")i.className="bx-section";t.fields.options[t.fields.length]=i}this.ProcessButtons()};this.TabMoveUp=function(){var t=document["form_settings_"+this.name];var e=t.tabs.selectedIndex;if(e>0){var s=BX.clone(this.aTabsEdit[e]);this.aTabsEdit[e]=BX.clone(this.aTabsEdit[e-1]);this.aTabsEdit[e-1]=s}jsSelectUtils.moveOptionsUp(t.tabs)};this.TabMoveDown=function(){var t=document["form_settings_"+this.name];var e=t.tabs.selectedIndex;if(e<t.tabs.length-1){var s=BX.clone(this.aTabsEdit[e]);this.aTabsEdit[e]=BX.clone(this.aTabsEdit[e+1]);this.aTabsEdit[e+1]=s}jsSelectUtils.moveOptionsDown(t.tabs)};this.TabEdit=function(){var t=document["form_settings_"+this.name];var e=t.tabs.selectedIndex;if(e<0)return;this.ShowTabSettings(this.aTabsEdit[e],function(){var i=document["tab_settings_"+s.name];s.aTabsEdit[e].name=i.tab_name.value;s.aTabsEdit[e].title=i.tab_title.value;t.tabs[e].text=i.tab_name.value})};this.TabAdd=function(){this.ShowTabSettings({name:"",title:""},function(){var t="tab_"+Math.round(Math.random()*1e6);var e=document["tab_settings_"+s.name];s.aTabsEdit[s.aTabsEdit.length]={id:t,name:e.tab_name.value,title:e.tab_title.value,fields:[]};var i=document["form_settings_"+s.name];i.tabs[i.tabs.length]=new Option(e.tab_name.value,t,true,true);s.OnSettingsChangeTab()})};this.TabDelete=function(){var t=document["form_settings_"+this.name];var e=t.tabs.selectedIndex;if(e<0)return;var s;for(s in this.aTabsEdit[e].fields){this.aAvailableFields[this.aTabsEdit[e].fields[s].id]=this.aTabsEdit[e].fields[s];jsSelectUtils.addNewOption(t.all_fields,this.aTabsEdit[e].fields[s].id,this.aTabsEdit[e].fields[s].name,true,false)}this.HighlightSections(t.all_fields);this.aTabsEdit=BX.util.deleteFromArray(this.aTabsEdit,e);t.tabs.remove(e);if(t.tabs.length>0){s=e<t.tabs.length?e:t.tabs.length-1;t.tabs[s].selected=true;this.OnSettingsChangeTab()}else{jsSelectUtils.deleteAllOptions(t.fields);this.ProcessButtons()}};this.ShowTabSettings=function(t,e){var s=this.tabSettingsWnd;if(!s){this.tabSettingsWnd=s=new BX.CDialog({content:'<form name="tab_settings_'+this.name+'">'+'<table width="100%">'+"<tr>"+'<td width="50%" align="right">'+this.vars.mess.tabSettingsName+"</td>"+'<td><input type="text" name="tab_name" size="30" value="" style="width:90%"></td>'+"</tr>"+"<tr>"+'<td align="right">'+this.vars.mess.tabSettingsCaption+"</td>"+'<td><input type="text" name="tab_title" size="30" value="" style="width:90%"></td>'+"</tr>"+"</table>"+"</form>",title:this.vars.mess.tabSettingsTitle,width:this.vars.tabSettingWndSize.width,height:this.vars.tabSettingWndSize.height,resize_id:"InterfaceFormTabSettingWnd"})}s.ClearButtons();s.SetButtons([{title:this.vars.mess.tabSettingsSave,action:function(){e();this.parentWindow.Close()}},BX.CDialog.prototype.btnCancel]);s.Show();var i=document["tab_settings_"+this.name];i.tab_name.value=t.name;i.tab_title.value=t.title;i.tab_name.focus()};this.ShowFieldSettings=function(t,e){var s=this.fieldSettingsWnd;if(!s){this.fieldSettingsWnd=s=new BX.CDialog({content:'<form name="field_settings_'+this.name+'">'+'<table width="100%">'+"<tr>"+'<td width="50%" align="right" id="field_name_'+this.name+'"></td>'+'<td><input type="text" name="field_name" size="30" value="" style="width:90%"></td>'+"</tr>"+"</table>"+"</form>",width:this.vars.fieldSettingWndSize.width,height:this.vars.fieldSettingWndSize.height,resize_id:"InterfaceFormFieldSettingWnd"})}s.SetTitle(t.type&&t.type=="section"?this.vars.mess.sectSettingsTitle:this.vars.mess.fieldSettingsTitle);BX("field_name_"+this.name).innerHTML=t.type&&t.type=="section"?this.vars.mess.sectSettingsName:this.vars.mess.fieldSettingsName;s.ClearButtons();s.SetButtons([{title:this.vars.mess.tabSettingsSave,action:function(){e();this.parentWindow.Close()}},BX.CDialog.prototype.btnCancel]);s.Show();var i=document["field_settings_"+this.name];i.field_name.value=t.name;i.field_name.focus()};this.FieldEdit=function(){var t=document["form_settings_"+this.name];var e=t.tabs.selectedIndex;var i=t.fields.selectedIndex;if(e<0||i<0)return;this.ShowFieldSettings(this.aTabsEdit[e].fields[i],function(){var a=document["field_settings_"+s.name];s.aTabsEdit[e].fields[i].name=a.field_name.value;t.fields[i].text=a.field_name.value})};this.FieldAdd=function(){var t=document["form_settings_"+this.name];var e=t.tabs.selectedIndex;if(e<0)return;this.ShowFieldSettings({name:"",type:"section"},function(){var i="field_"+Math.round(Math.random()*1e6);var a=document["field_settings_"+s.name];s.aTabsEdit[e].fields[s.aTabsEdit[e].fields.length]={id:i,name:a.field_name.value,type:"section"};var n=new Option(a.field_name.value,i,true,true);n.className="bx-section";t.fields[t.fields.length]=n;s.ProcessButtons()})};this.FieldsMoveUp=function(){var t=document["form_settings_"+this.name];var e=t.tabs.selectedIndex;var s=t.fields.length;for(var i=0;i<s;i++){if(t.fields[i].selected&&i>0&&t.fields[i-1].selected==false){var a=BX.clone(this.aTabsEdit[e].fields[i]);this.aTabsEdit[e].fields[i]=BX.clone(this.aTabsEdit[e].fields[i-1]);this.aTabsEdit[e].fields[i-1]=a;var n=new Option(t.fields[i].text,t.fields[i].value);var l=new Option(t.fields[i-1].text,t.fields[i-1].value);n.className=t.fields[i].className;l.className=t.fields[i-1].className;t.fields[i]=l;t.fields[i].selected=false;t.fields[i-1]=n;t.fields[i-1].selected=true}}};this.FieldsMoveDown=function(){var t=document["form_settings_"+this.name];var e=t.tabs.selectedIndex;var s=t.fields.length;for(var i=s-1;i>=0;i--){if(t.fields[i].selected&&i<s-1&&t.fields[i+1].selected==false){var a=BX.clone(this.aTabsEdit[e].fields[i]);this.aTabsEdit[e].fields[i]=BX.clone(this.aTabsEdit[e].fields[i+1]);this.aTabsEdit[e].fields[i+1]=a;var n=new Option(t.fields[i].text,t.fields[i].value);var l=new Option(t.fields[i+1].text,t.fields[i+1].value);n.className=t.fields[i].className;l.className=t.fields[i+1].className;t.fields[i]=l;t.fields[i].selected=false;t.fields[i+1]=n;t.fields[i+1].selected=true}}};this.FieldsAdd=function(){var t=document["form_settings_"+this.name];var e=t.tabs.selectedIndex;if(e==-1)return;var s=this.aTabsEdit[e].fields;var i=t.all_fields.length,a;for(a=0;a<i;a++)if(t.all_fields[a].selected)s[s.length]={id:t.all_fields[a].value,name:t.all_fields[a].text,type:this.aAvailableFields[t.all_fields[a].value].type};jsSelectUtils.addSelectedOptions(t.all_fields,t.fields,false,false);jsSelectUtils.deleteSelectedOptions(t.all_fields);for(a=0,i=t.fields.length;a<i;a++)if(s[a].type=="section")t.fields[a].className="bx-section";this.ProcessButtons()};this.FieldsDelete=function(){var t=document["form_settings_"+this.name];var e=t.tabs.selectedIndex;if(e==-1)return;var s=t.fields.length;var i=0;for(var a=0;a<s;a++){if(t.fields[a].selected){this.aAvailableFields[t.fields[a].value]=this.aTabsEdit[e].fields[a-i];this.aTabsEdit[e].fields=BX.util.deleteFromArray(this.aTabsEdit[e].fields,a-i);i++}}jsSelectUtils.addSelectedOptions(t.fields,t.all_fields,false,true);jsSelectUtils.deleteSelectedOptions(t.fields);this.HighlightSections(t.all_fields);this.ProcessButtons()};this.ProcessButtons=function(){var t=document["form_settings_"+this.name];t.add_btn.disabled=t.all_fields.selectedIndex==-1||t.tabs.selectedIndex==-1;t.del_btn.disabled=t.up_btn.disabled=t.down_btn.disabled=t.field_edit_btn.disabled=t.fields.selectedIndex==-1;t.tab_up_btn.disabled=t.tab_down_btn.disabled=t.tab_edit_btn.disabled=t.tab_del_btn.disabled=t.field_add_btn.disabled=t.tabs.selectedIndex==-1};this.HighlightSections=function(t){for(var e=0,s=t.length;e<s;e++)if(this.aAvailableFields[t[e].value].type=="section")t[e].className="bx-section"};this.SaveSettings=function(){var t={FORM_ID:this.name,action:"savesettings",sessid:this.vars.sessid,tabs:this.aTabsEdit};var e=document["form_settings_"+this.name];if(e["set_default_settings"]){t.set_default_settings=e.set_default_settings.checked?"Y":"N";t.delete_users_settings=e.delete_users_settings.checked?"Y":"N"}BX.ajax.post("/bitrix/components"+s.vars.component_path+"/settings.php",t,function(){s.Reload()})};this.EnableSettings=function(t){BX.ajax.get("/bitrix/components"+this.vars.component_path+"/settings.php?FORM_ID="+this.name+"&action=enable&enabled="+(t?"Y":"N")+"&sessid="+this.vars.sessid,function(){s.Reload()})};this.Reload=function(){if(this.vars.ajax.AJAX_ID!="")BX.ajax.insertToNode(this.vars.current_url+(this.vars.current_url.indexOf("?")==-1?"?":"&")+"bxajaxid="+this.vars.ajax.AJAX_ID,"comp_"+this.vars.ajax.AJAX_ID);else window.location=window.location.href}}